package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PackersActivityOtherAskBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.swgk.core.util.PickSelectEntity;

import com.sgb.kjwl.model.entity.packers.OtherAskTransmitEntity;
import com.sgb.kjwl.model.entity.packers.PackerNationEntity;
import com.sgb.kjwl.view.ui.adapter.packers.OtherAskAdapter;
import com.sgb.kjwl.viewmodel.packers.OtherAskViewModel;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/7/21 12:01
 * description: 其他要求的新增编辑页面
 * first:写在前面。对于有泛型的对象，其实我们需要的是把对象写出，或者直接赋予空对象，我们知道对于没有泛型进行注明的时候，系统会队此泛型进行泛型检查
 * 这个时候其实对于泛型本身，系统会把它当成一个object对象，其实这是错误的，首先我们需要做到的是能不强转就不强转，其次对于object对象对于内存中创建
 * 需要一定的空间，其实这也会造成一定程度的泄露。其实对于我们的对象来说，我们是并不需要这个object对象的，但是我们的这个类却为我们创建了这个对象，所
 * 以，其实这也违背了对于程序来说的一些方针，另外，对于强转这个问题，如果你有了解编码，反码，补码你就会知道，我记得之前有一个题目，我也不是记得很清楚
 * 大概意思是说一个正整数会有一种情况会小于0，这个就是说的是byte的补码问题，当byte越界也就是说是128时，系统会直接给出这个byte的补码，所以这个时候
 * 我们的这个正整数时小于0的，所以在非必须情况，我们需要在泛型检查的时候进行处理。
 * second:别说我为什么也没有泛型检查，因为我也是复制的=-=
 */
public class OtherAskActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    /**
     * 本页面对应的viewModel
     */
    OtherAskViewModel mModel;
    /**
     * 本页面对应的dataBinding
     */
    private PackersActivityOtherAskBinding mBinding;
    /**
     * 民族选择器
     */
    OptionsPickerView mNationOption;
    /**
     * 工龄选择的下标
     */
    private int mLengthOfServicePosition;
    /**
     * 健康状态的选择的下标
     */
    private int mHealthPosition;
    /**
     * 民族要求选择的下标
     */
    private int mNationPosition;
    /**
     * 性别要求选择的下标
     */
    private int mSexPosition;
    /**
     *  年龄要求选择的下标
     */
    private int mAgePosition;
    /**
     *  薪资发放选择的下标
     */
    private int mPayrollPaymentPosition;
    /**
     *  薪资管理选择的下标
     */
    private int mSalaryManagementPosition;
    /**
     * 用来传递的实体对象，也是用来得到当前界面初始值的对象
     */
    private OtherAskTransmitEntity mEntity;
    /**
     * 最下面添加字段的adapter
     */
    private OtherAskAdapter mAdapter;
    /**
     * 设置adapter的集合，更新此集合来更换adapter里面的数据
     */
    private List<OtherAskTransmitEntity.AddFiled> mField;

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.packers_activity_other_ask);

        mBinding.setLifecycleOwner(this);
        mModel = new ViewModelProvider(this).get(OtherAskViewModel.class);


        initListener();
        initLiveData();
        initRecyclerView();
        initData();
    }

    /**
     * 初始化recyclerView
     */
    private void initRecyclerView() {
        mField = new ArrayList<>();
        mAdapter = new OtherAskAdapter(this,mField);
        mBinding.rvAddField.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvAddField.setAdapter(mAdapter);

        mAdapter.setClick(new RecyclerClick<OtherAskTransmitEntity.AddFiled>() {
            @Override
            public void includePositionClick(OtherAskTransmitEntity.AddFiled entity, int position) {
                mField.remove(position);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 初始化数据(请求网络数据，获取本地数据等)
     */
    private void initData() {

        //这里有可能会从上一个界面传回来数据，也就是说我们需要有一个初始值。然后下面我们判断了这个初始值是否为空，如果为空我们就给他设置一个对象，如果不为空我们需要在这里给各个控件设置数据
        mEntity = (OtherAskTransmitEntity) getIntent().getSerializableExtra("result");
        if (mEntity == null){
            mEntity = new OtherAskTransmitEntity();
        }else {
            if (!TextUtils.isEmpty(mEntity.getConditionsOfResidence())){
                mBinding.iitConditionsOfResidence.setContent(mEntity.getConditionsOfResidence());
            }

            if (!TextUtils.isEmpty(mEntity.getEatingConditions())){
                mBinding.iitEatingConditions.setContent(mEntity.getEatingConditions());
            }

            if (!TextUtils.isEmpty(mEntity.getSalaryManagementName())){
                mBinding.iitSalaryManagement.setContent(mEntity.getSalaryManagementName());
            }

            if (!TextUtils.isEmpty(mEntity.getPayrollPaymentName())){
                mBinding.iitPayrollPayment.setContent(mEntity.getPayrollPaymentName());
            }

            if (!TextUtils.isEmpty(mEntity.getProfessionalClothing())){
                mBinding.iitProfessionalClothing.setContent(mEntity.getProfessionalClothing());
            }

            if (!TextUtils.isEmpty(mEntity.getSexRequirementsName())){
                mBinding.iitSexRequirements.setContent(mEntity.getSexRequirementsName());
            }

            if (!TextUtils.isEmpty(mEntity.getAgeRequirementsName())){
                mBinding.iitAgeRequirements.setContent(mEntity.getAgeRequirementsName());
            }

            if (!TextUtils.isEmpty(mEntity.getSexRequirementsName())){
                mBinding.iitSexRequirements.setContent(mEntity.getSexRequirementsName());
            }

            if (!TextUtils.isEmpty(mEntity.getEthnicRequirementsName())){
                mBinding.iitNationRequirements.setContent(mEntity.getEthnicRequirementsName());
            }

            if (!TextUtils.isEmpty(mEntity.getLengthOfServiceName())){
                mBinding.iitLengthOfServiceRequirements.setContent(mEntity.getLengthOfServiceName());
            }

            if (!TextUtils.isEmpty(mEntity.getMedicalCertificateName())){
                mBinding.iitEmdicalCertificate.setContent(mEntity.getMedicalCertificateName());
            }

            if (!TextUtils.isEmpty(mEntity.getPerformanceRequirements())){
                mBinding.iitSelectPay.setContent(mEntity.getPerformanceRequirements());
            }

            if (mEntity.getAddField() != null && mEntity.getAddField().size() > 0){
                mField.addAll(mEntity.getAddField());
                mAdapter.notifyDataSetChanged();
            }
        }

        mModel.getNationRequirements();
    }

    /**
     * 初始化LiveData 在这里设置好得到数据之后的逻辑
     */
    private void initLiveData() {
        //请注意这下面三个liveData的作用主要是处理加载进度条的作用，这里进行统一处理就不需要在viewModel里面传入上下文
        mModel.loadingLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.showProgressDialog(OtherAskActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));
        mModel.successLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.dismissProgressDialog());
        mModel.errorLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.dismissProgressDialog());

        //民族的livedata
        mModel.mNationLiveDta.observe(this, this::setNation);
    }

    /**
     * 这个方法的主要作用是是设置监听，包括各个控件的点击监听和控件自带监听
     */
    private void initListener() {
        mBinding.iitAgeRequirements.setOnWholeItemClickListener(this);
        mBinding.iitSalaryManagement.setOnWholeItemClickListener(this);
        mBinding.iitPayrollPayment.setOnWholeItemClickListener(this);
        mBinding.iitSexRequirements.setOnWholeItemClickListener(this);
        mBinding.iitNationRequirements.setOnWholeItemClickListener(this);
        mBinding.iitLengthOfServiceRequirements.setOnWholeItemClickListener(this);
        mBinding.iitEmdicalCertificate.setOnWholeItemClickListener(this);
        mBinding.ivAddAnsAddField.setOnClickListener(this);
        mBinding.btnRelease.setOnClickListener(this);
    }

    /**
     * 请注意,在写出if语句之后一定要写出大括号，即使它只有一行，以此来提升代码可读性
     * @param v 点击时间的view
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_add_ans_add_field) {
            if (mField.size() <= 2){
                mField.add(new OtherAskTransmitEntity.AddFiled());
                mAdapter.notifyDataSetChanged();
            }else {
                MToast.showToast(this,"最多只能新增三个字段");
            }
        }else if (v.getId() == R.id.btn_release){
            for (int i = 0; i < mAdapter.getDatas().size(); i++) {
                if (TextUtils.isEmpty(mAdapter.getDatas().get(i).getName())){
                    MToast.showToast(this,"请输入字段名称");
                    return;
                }

                if (TextUtils.isEmpty(mAdapter.getDatas().get(i).getContent())){
                    MToast.showToast(this,"请输入字段内容");
                    return;
                }
            }
            onBackPressed();
        }
    }

    /**
     * 请注意在使用switch case的时候，请一定要写出default，即使它什么也不做
     * @param view 点击事件的view
     */
    @Override
    public void OnWholeItemClick(View view) {
        int id = view.getId();
        if (id == R.id.iit_emdical_certificate) {
            showEmpirical(mModel.getMedicalCertificate());
        } else if (id == R.id.iit_length_of_service_requirements) {
            showLengthOfService(mModel.getLengthOfServiceRequirements());
        } else if (id == R.id.iit_nation_requirements) {
            showNation();
        } else if (id == R.id.iit_age_requirements) {
            showAge(mModel.getAgeRequirements());
        } else if (id == R.id.iit_sex_requirements) {
            showSex(mModel.getSexRequirements());
        } else if (id == R.id.iit_payroll_payment) {
            showPayroll(mModel.getPayrollPayment());
        } else if (id == R.id.iit_salary_management) {
            showSalary(mModel.getSalaryManagement());
        }
    }

    /**
     * 健康证明选择器
     * @param list 健康选择器的数据
     */
    public void showEmpirical(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitEmdicalCertificate.setContent(list.get(options1).getProjectscale());
            mHealthPosition = options1;
            mEntity.setMedicalCertificateName(list.get(options1).getProjectscale());
            mEntity.setMedicalCertificateNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mHealthPosition);
        mApplyOption.setTitleText("选择健康证明要求");
        mApplyOption.show();
    }

    /**
     * 工龄要求选择器
     * @param list 工龄要求选择器的数据
     */
    public void showLengthOfService(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitLengthOfServiceRequirements.setContent(list.get(options1).getProjectscale());
            mLengthOfServicePosition = options1;
            mEntity.setLengthOfServiceName(list.get(options1).getProjectscale());
            mEntity.setLengthOfServiceNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mLengthOfServicePosition);
        mApplyOption.setTitleText("选择工龄要求");
        mApplyOption.show();
    }

    /**
     * 民族要求选择器
     * @param list 民族要求选择器的数据
     */
    public void setNation(List<PackerNationEntity> list) {
        mNationOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitNationRequirements.setContent(list.get(options1).getProjectscale());
            mNationPosition = options1;
            mEntity.setEthnicRequirementsName(list.get(options1).getProjectscale());
            mEntity.setEthnicRequirementsNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mNationOption.setPicker(list);
        mNationOption.setSelectOptions(mNationPosition);
        mNationOption.setTitleText("选择民族要求");
    }

    public void showNation(){
        mNationOption.show();


    }

    /**
     * 性别要求选择器
     * @param list 性别要求选择器的数据
     */
    public void showSex(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitSexRequirements.setContent(list.get(options1).getProjectscale());
            mSexPosition = options1;
            mEntity.setSexRequirementsName(list.get(options1).getProjectscale());
            mEntity.setSexRequirementsNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mSexPosition);
        mApplyOption.setTitleText("选择性别要求");
        mApplyOption.show();
    }

    /**
     * 年龄要求选择器
     * @param list 年龄要求选择器的数据
     */
    public void showAge(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitAgeRequirements.setContent(list.get(options1).getProjectscale());
            mAgePosition = options1;
            mEntity.setAgeRequirementsName(list.get(options1).getProjectscale());
            mEntity.setAgeRequirementsNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mAgePosition);
        mApplyOption.setTitleText("选择年龄要求");
        mApplyOption.show();
    }

    /**
     * 薪资发放方式选择器
     * @param list 薪资发放方式选择器的数据
     */
    public void showPayroll(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitPayrollPayment.setContent(list.get(options1).getProjectscale());
            mPayrollPaymentPosition = options1;
            mEntity.setPayrollPaymentName(list.get(options1).getProjectscale());
            mEntity.setPayrollPaymentNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mPayrollPaymentPosition);
        mApplyOption.setTitleText("选择薪资发放方式");
        mApplyOption.show();
    }

    /**
     * 薪资管理方式选择器
     * @param list 薪资管理方式选择器的数据
     */
    public void showSalary(List<PickSelectEntity> list) {
        OptionsPickerView mApplyOption = new OptionsPickerBuilder(OtherAskActivity.this, (options1, options2, options3, v) -> {
            mBinding.iitSalaryManagement.setContent(list.get(options1).getProjectscale());
            mSalaryManagementPosition = options1;
            mEntity.setSalaryManagementName(list.get(options1).getProjectscale());
            mEntity.setSalaryManagementNo(list.get(options1).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        mApplyOption.setPicker(list);
        mApplyOption.setSelectOptions(mSalaryManagementPosition);
        mApplyOption.setTitleText("选择薪资管理方式");
        mApplyOption.show();
    }

    /**
     * 这是返回键按钮，当我们做返回之前(super)
     */
    @Override
    public void onBackPressed() {
        if (mEntity != null){
            mEntity.setConditionsOfResidence(mBinding.iitConditionsOfResidence.getContent());
            mEntity.setEatingConditions(mBinding.iitEatingConditions.getContent());
            mEntity.setProfessionalClothing(mBinding.iitProfessionalClothing.getContent());
            mEntity.setPerformanceRequirements(mBinding.iitSelectPay.getContent());
            mEntity.setAddField(mAdapter.getDatas());
            Intent intent = new Intent();
            intent.putExtra("result",mEntity);
            setResult(1,intent);
        }
        super.onBackPressed();
    }
}
